﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using WCell.Constants;
using WCell.Constants.World;
using WCell.Core.DBC;
using WCell.MPQTool;
using WCell.RealmServer;
using WCell.Tools.Maps.Parsing.ADT;
using WCell.Util.Toolshed;

namespace WCell.Tools.Maps
{
    public static class ZoneTileSetReader
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        private static MpqManager manager;
        private const string baseDir = "WORLD\\maps\\";

        public static ZoneTileSet[] ReadZoneIds()
        {
            var wowRootDir = DBCTool.FindWowDir(null);
            manager = new MpqManager(wowRootDir);

            var entryList = DBCMapReader.GetMapEntries();
            if (entryList == null)
            {
                Console.WriteLine("Error retrieving Map Entries.");
                return null;
            }

			var tiles = new ZoneTileSet[(int)MapId.End];
			var count = 0;

            foreach (var dbcMapEntry in entryList)
            {
                var dir = dbcMapEntry.MapDirName;
                var wdtDir = Path.Combine(baseDir, dir);
                var wdtName = dir;

                var wdt = WDTParser.Process(manager, wdtDir, wdtName);
                if (wdt == null) continue;

                if ((wdt.Header.Header1 & WDTFlags.GlobalWMO) == 0)
                {
                    var tileSet = new ZoneTileSet();
					tiles[dbcMapEntry.Id] = tileSet;

                	ZoneGrid grid;

                    // Read in the Tiles
                    for (var y = 0; y < 64; y++)
                    {
                        for (var x = 0; x < 64; x++)
                        {
                            if (!wdt.TileProfile[x, y]) continue;
                        	++count;
                            var adtName = string.Format("{0}_{1:00}_{2:00}", wdtName, x, y);
                            var adt = ADTParser.Process(manager, wdtDir, adtName);
                            if (adt == null) continue;

							tileSet.ZoneGrids[x, y] = grid = new ZoneGrid(new uint[TerrainConstants.ChunksPerTileSide, TerrainConstants.ChunksPerTileSide]);
                            // Read in the TileChunks 
                            for (var j = 0; j < 16; j++)
                            {
                                for (var i = 0; i < 16; i++)
                                {
                                    var areaId = adt.MapChunks[i, j].AreaId;
									if (Enum.IsDefined(typeof(ZoneId), areaId))
                                	{
                                		grid.ZoneIds[i, j] = areaId;
                                	}
									else
                                	{
                                		grid.ZoneIds[i, j] = 0;
                                	}
                                }
							}
							//return tiles;
                        }
					}
                }
				else
                {
                	log.Info("Could not read Zones from WMO: " + (MapId)dbcMapEntry.Id);
                }
            }

        	log.Info("Exported {0} ZoneTileSets.", count);

            return tiles;
        }

        

        

    }
}
